﻿using Super_Management_Mayhem.GameRoot;
using Super_Management_Mayhem.Screens.PlayerSelection;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.SelectPlayers
{
    public class SelectPlayers : BaseMenuScreen<Config.Menu>
    {
        private Button[] _buttons;
        private int _currentButtonSelected;

        public enum PlayerCount
        {
            One,
            Two,
            Three,
            Four,
            Six,
            Eight,
        }

        public PlayerCount Players
        {
            get
            {
                return (PlayerCount) _currentButtonSelected;
            }
        }

        public override void LoadContent()
        {
            

            var buttonContainer = new Container(Game.Options.ScreenSize.X / 2 - 2, Game.Options.ScreenSize.Y / 2 - 2);

            _currentButtonSelected = 0;

            _buttons = new[] {
                                 new Button(new TE_Vector2(-16.5f, -5), "One player"), 
                                 new Button(new TE_Vector2(0.5f, -5), "Two players"),
                                 new Button(new TE_Vector2(-16.5f, 0), "Three players"),
                                 new Button(new TE_Vector2(0.5f, 0), "Four players"),
                                 new Button(new TE_Vector2(-16.5f, 5), "Six players"),
                                 new Button(new TE_Vector2(0.5f, 5), "Eight players"),
                             };

            _buttons[0].Select();

            buttonContainer.AddChildren(_buttons);

            AddChildren(buttonContainer);

            AddChildren(Title.Gray("Select a party type", Game.Options.ScreenSize.Offset(-4, -4)));

            base.LoadContent();
        }

        void MoveSelectionUp()
        {
            if(_currentButtonSelected > 1)
            {
                _buttons[_currentButtonSelected].Unselect();
                _currentButtonSelected -= 2;
                _buttons[_currentButtonSelected].Select();
            }
        }

        void MoveSelectionDown()
        {
            if(_currentButtonSelected < _buttons.Length - 2)
            {
                _buttons[_currentButtonSelected].Unselect();
                _currentButtonSelected += 2;
                _buttons[_currentButtonSelected].Select();
            }
        }

        void MoveSelectionLeft()
        {
            if(_currentButtonSelected > 0)
            {
                _buttons[_currentButtonSelected].Unselect();
                _currentButtonSelected--;
                _buttons[_currentButtonSelected].Select();
            }
        }

        void MoveSelectionRight()
        {
            if(_currentButtonSelected < _buttons.Length - 1)
            {
                _buttons[_currentButtonSelected].Unselect();
                _currentButtonSelected++;
                _buttons[_currentButtonSelected].Select();
            }
        }

        public override void UpKey(int id)
        {
            MoveSelectionUp();
        }

        public override void DownKey(int id)
        {
            MoveSelectionDown();
        }

        public override void LeftKey(int id)
        {
            MoveSelectionLeft();
        }

        public override void RightKey(int id)
        {
            MoveSelectionRight();
        }

        public override void AcceptKey(int id)
        {
            ChangeScreen(-1, Config.Menu.SelectGameMode);
        }

        public override void CancelKey(int id)
        {
        }
    }
}
